Thread: Any problem with casting like this: (char[]){0,1,2,3,4)?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    101

    Any problem with casting like this: (char[]){0,1,2,3,4)?

    I was getting "excess elements in scalar initializer" errors from GCC from the following code, even though it still compiled and ran fine:

    Code:
    void *array[] = {
    	(char){0,1,2,3,4},
    	(char){5,6,7,8,9}
    };
    I was able to get rid of the warnings by doing this:

    Code:
    void *array[] = {
    	(char[]){0,1,2,3,4},
    	(char[]){5,6,7,8,9}
    };
    Is this just because I'm informing GCC that I the data should be initialized as an array? Again, the previous code worked even with the warnings.

    Anything I should be worried about?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the second one looks ok according to the GCC manual for that nonstandard extension. the first one says 'you told me the next value is a char, but you gave me an array initializer'

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I find it hard to believe that the first one would run fine if you tried to access the elements.

    Try printing the elements: surely this crashes?

    Code:
       for (i = 0; i < 2; i++)
       {
           char*  arr = array[i];
           printf("\n");
           for (j = 0; j < 5; j++)
           {
              printf("%d ", arr[j]);          
           }
       }
    Your char cast tells GCC to turn 0,1,2,3,4 into a single char. It doesn't matter that this value will then be converted to a void*, it's no more sensible than doing:

    Code:
     char c = {0,1,2,3,4};
    The GCC I tried this on took the first value and ignored the rest, then interpreted that char as a pointer (with a warning: "initialization makes pointer from integer without a cast"). Don't know if all GCCs behave the same.

    That's the equivalent of:
    Code:
    void *array[] = {
        (void*)0,
        (void*)5
    };
    Which is definitely not what you want.

    So yes, you should be worried whenever you see this warning. It's pretty annoying that this is a warning rather than an error...

    Quote Originally Posted by dmh2000 View Post
    the second one looks ok according to the GCC manual for that nonstandard extension.
    Is it nonstardard? I think it's C99.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Is this the sort of thing that you are after?
    Code:
        char arr1[5] = {0,1,2,3,4};
        char arr2[5] = {5,6,7,8,9};
        void *array[] = {arr1, arr2};
    
    
        int i,j;
        for (i = 0; i < 2; i++)
        {
            char (*s_array)[5] = array[i];
    
    
            printf("\n");
            for (j = 0; j < 5; j++)
            {
               printf("%d ", (*s_array)[j]);
            }
        }
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Quote Originally Posted by smokeyangel View Post
    I find it hard to believe that the first one would run fine if you tried to access the elements.

    Try printing the elements: surely this crashes?
    Actually yes, it does. Sorry, that was a bad example. The example I should have used was in fact a struct:

    Code:
    struct test{
    	char a[5];
    	char b[5];
    } test;
    
    memcpy(&test, &((struct test){0,1,2,3,4,5,6,7,8,9}), sizeof(struct test));
    That was giving me a warning in GCC even though the code compiled and ran. I realized, however, that I made a mistake and should have done this instead:

    Code:
    memcpy(&test, &((struct test){{0,1,2,3,4},{5,6,7,8,9}}), sizeof(struct test));

    Quote Originally Posted by Click_here View Post
    Is this the sort of thing that you are after?
    Code:
        char arr1[5] = {0,1,2,3,4};
        char arr2[5] = {5,6,7,8,9};
        void *array[] = {arr1, arr2};
    
    
        int i,j;
        for (i = 0; i < 2; i++)
        {
            char (*s_array)[5] = array[i];
    
    
            printf("\n");
            for (j = 0; j < 5; j++)
            {
               printf("%d ", (*s_array)[j]);
            }
        }
    I was hoping to avoid having to first initialize each "secondary" array individually, then initialize the pointer array with the secondary arrays, if that makes sense.

    Anyway, I found this:

    Using the GNU Compiler Collection (GCC)

    Apparently the (char[]) cast is valid C99. I compiled my code in both GCC and Clang, and got no warnings.

    I'm using GCC 4.7.2 and Apple's Clang/LLVM 3.1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting a 32-bit int as a char array
    By Invariant in forum C Programming
    Replies: 3
    Last Post: 01-28-2010, 07:14 AM
  2. int to char casting/itoa
    By dayknight in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 03:16 PM
  3. Casting char as int problem
    By Malek in forum C++ Programming
    Replies: 14
    Last Post: 05-02-2003, 09:33 PM
  4. assigning and casting (char) to (int)
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2002, 12:53 PM
  5. casting int to char
    By matheo917 in forum C++ Programming
    Replies: 13
    Last Post: 04-05-2002, 10:38 PM